﻿package nl.haemers.utils 
{
	import flash.errors.IllegalOperationError;	
	import flash.text.Font;
	import flash.utils.getDefinitionByName;	
	
	/**
	 * ...
	 * @author Jonathan Haemers
	 */
	public class FontManager
	{
		private static var _instance		:FontManager;
		private var _fonts					:Array;
		private static var _nameSpace		:String;
		
		
		public function FontManager()
		{
			if (_instance != null) throw new IllegalOperationError("Use FontManager.getInstance() to instantiate.");
		}
		
		public static function getInstance() : FontManager
		{
			if (_instance == null)_instance = new FontManager();
			
			return _instance;
		}
		
		public static function set nameSpace(value:String) : void
		{
			_nameSpace = value;
		}
		
		public function registerFont( fontName:String ) : void
		{
			var font:Class = getDefinitionByName( _nameSpace + "." + fontName ) as Class;
			
			Font.registerFont( font );
			_fonts = Font.enumerateFonts(false);
		}
		
		public function getFont( fontName:String, fontStyle:String="regular" ) : String
		{
			for (var i:int = 0; i < _fonts.length; i++)
			{
				//trace(fontName+" == "+_fonts[i].fontName+" && "+fontStyle+" == "+_fonts[i].fontStyle)
				if (fontName == _fonts[i].fontName && fontStyle == _fonts[i].fontStyle)
				{
					//trace("returning " + _fonts[i].fontName);
					return _fonts[i].fontName;
				}
			}
			//trace("returning Arial");
			return "Arial";
		}
		
		
		
	}

}